//
//  RegisterViewController.m
//  Inforcollection
//
//  Created by macbook on 15/12/8.
//  Copyright © 2015年 oceansoft.liw. All rights reserved.
//

#import "RegisterViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>


@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initViews];

    [self loadWebView];
    
    [self ocGetjsData];
    
}
-(void)initViews
{
    [self setNaviBarTitle:@"注册"];
}
-(void)ocGetjsData
{
    JSContext *context = [self.currentWebView  valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    context[@"myFunction"]=^()
    {
        NSArray *args = [JSContext currentArguments];
        
        if (args.count>0)
        {
            JSValue *jsVal = args[0];
            
            _userName = [jsVal toString];
            
            [SharedNotifiCenter postNotificationName:KgetUserNameWithRegister object:nil userInfo:@{@"userName":_userName}];
            
            [self toLoginVCWithUserName:[args[0] toString]];
            
            [self.view.window makeToast:@"注册成功" duration:2.0];
        }
        for (JSValue *jsVal in args)
        {
            NSLog(@"%@", jsVal);
        }
    };
}
//-(void)loadWebView
//{    
//    NSURL* url = [NSURL URLWithString:[APIAdress getRegister]];
//    
//    NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
//    
//    [self.currentWebView loadRequest:request];
//}

-(void)toLoginVCWithUserName:(NSString *)currentUserName
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}

//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//{
//    NSString *deptId = [self.currentWebView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
//    
//    if (deptId.length>0)
//    {
//        NSLog(@"%@",@"注册成功");
//        return NO;
//    }
//    else
//    {
//        NSLog(@"%@",@"注册失败");
//    }
//    return YES;
//}

//-(void)webViewDidStartLoad:(UIWebView *)webView
//{
//    //{var _val=document.getElementById('unitname').value;return _val;}
//    NSString *script = @"\
//    var _script = document.createElement(\"script\");\
//    _script.type=\"text/javascript\";\
//    _script.text=\"function myFunction(){return userID;}\";\
//    document.getElementsByTagName('head')[0].appendChild(_script);";
//    
//    [self.currentWebView stringByEvaluatingJavaScriptFromString:script];
//}


@end
